package UILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.RentalContainer;
import ModelLayer.Machine;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
 * Write a description of class rentalUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RentalUI
{
    private RentalController rentalCon;
    private Scanner reader;

    public RentalUI()
    {
        rentalCon = new RentalController();
        reader = new Scanner(System.in);
        start();
    }

    public void start()
    {
        rentalMenu();
    }
    
    public void rentalMenu()
    {
            boolean exit = false;
            while(!exit)
        {
            int choice = writeRentalMenu();
            
            switch(choice)
                {
                case 1:
                   createMachine();
                   break;
                case 2:
                    printMachineList();
                    break;
                case 3:
                    rentMachine();
                    break;
                case 4:
                    machineDelivered();
                    break;
                case 5:
                    removeMachine();
                    break;
                case 0:
                    exit = true;
                    break;
                }
        }
    }
    
    public int writeRentalMenu()
    {
        int choice = 50;
        while (choice == 50)
        {
            System.out.println("\f*** RentalMenu ***");
            System.out.println(" (1)     Create Machine");
            System.out.println(" (2)     print Machine List");
            System.out.println(" (3)     Rent Machine");
            System.out.println(" (4)     Deliver Machine");
            System.out.println(" (5)     Delete Machine");
            System.out.println(" (0)     Return To Main Menu");
            System.out.print("\n Make your choice: ");
            choice = inputNumber();
        }
        return choice;
    }
    
    public int inputMachineNo()
    {   
        int id = 0;
        while(id == 0)
        {
            System.out.print("\fWrite machineNo:  ");
            id = inputNumber();
        }//end while
        return id;
    }
    
    private int inputNumber()
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            int no = keyboard.nextInt();
            if (no < 0)
            {
                return 0;
            }
            return no;
        }
        catch (Exception e)
        {
            System.out.println(" You need to write a positive number -  Try again ");
            String input = keyboard.nextLine();
            return 0;
        }
    }
    
    private String inputText()
    {
        Scanner keyboard = new Scanner(System.in);
        return keyboard.nextLine();
    }
    
    private String inputName()
    {
        System.out.print(" Write Name:  ");
        return inputText();
    }
    
    private String inputDescription()
    {
        System.out.print(" Write Description:  ");
        return inputText();
    }
       
    private int inputPrice()
    {
        System.out.print(" Write Price:  ");
        int price = UIInput.inputInt();
        return price;
    }
    
    private void createMachine()
    {
        System.out.println("\f");
        String name = inputName();
        String description = inputDescription();
        int price = inputPrice();
        rentalCon.createMachine(name, description, false, price);
    }
    
    private void printMachineList()
    {
        HashMap<Integer, Machine> container = rentalCon.getRentalContainer();
        Set<Integer> s = container.keySet(); 
        for(Integer maskinNr : s)
        {
            Machine m = container.get(maskinNr);
            System.out.println(m.getName());
            System.out.println(m.getDescription());
            System.out.println("Maskin Nr: " + m.getMachineNo());
            System.out.println("leje pris: " + m.getPrice());
            System.out.println("Udlejet: " + m.getMachineStatus());
            System.out.println("------------------");
        }
        back();
    }
    
    private void rentMachine()
    {
        try
        {
            int id = inputMachineNo();
            rentalCon.rentMachine(id);
            System.out.println(" Machine has been rented");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        back();
    }
    private void machineDelivered()
    {
        try
        {
            int id = inputMachineNo();
            rentalCon.machineDelivered(id);
            System.out.println(" Machine has been delivered");
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        back();
    }
    private void removeMachine()
    {
        int id = inputMachineNo();
        rentalCon.removeMachine(id);
        System.out.println(" Machine has been deleted");
        back();
    }
    private void back()
    {
        System.out.println("");
        System.out.print("Press 'enter' to get back to menu");
        String i = reader.nextLine();
    }
    
}
